﻿using System.Collections.Generic;

namespace EzDeploy.Core
{
    public class AssemblyFilterProcessor : IAssemblyFilterProcessor
    {
        public DeployGridItem Filter(IEnumerable<ExludedItem> excludes, DeployGridItem item)
        {
            string assemblyName = item.AssemblyName.Name.ToLowerInvariant();
            string assemblyCompany = item.AssemblyName.Attributes.Count > 0 ? item.AssemblyName.Attributes["AssemblyCompany"].ToLowerInvariant() : string.Empty;

            foreach (var exclude in excludes)
            {
                bool processed = false;

                if (!string.IsNullOrEmpty(exclude.Value))
                {
                    switch(exclude.MatchType)
                    {
                        case MatchType.Equals:
                            if (assemblyName == exclude.Value.ToLowerInvariant())
                            {
                                item.DeploymentType = DeploymentType.Exclude;
                                processed = true;
                            }
                            break;
                        case MatchType.StartWith:
                            if (assemblyName.StartsWith(exclude.Value.ToLowerInvariant()))
                            {
                                item.DeploymentType = DeploymentType.Exclude;
                                processed = true;
                            }
                            break;
                        case MatchType.EndWith:
                            if (assemblyName.EndsWith(exclude.Value.ToLowerInvariant()))
                            {
                                item.DeploymentType = DeploymentType.Exclude;
                                processed = true;
                            }
                            break;
                        case MatchType.Any:
                            if (assemblyName.IndexOf(exclude.Value.ToLowerInvariant()) != -1)
                            {
                                item.DeploymentType = DeploymentType.Exclude;
                                processed = true;
                            }
                            break;
                        case MatchType.AssemblyCompany:
                            if (assemblyCompany == exclude.Value.ToLowerInvariant())
                            {
                                item.DeploymentType = DeploymentType.Exclude;
                                processed = true;
                            }
                            break;
                    }
                }

                if (processed)
                    break;
            }

            return item;
        }
    }
}
